/*
题目描述
已有a、b两个链表，每个链表中的节点包括学号、成绩，且a、b两个链表中的节点均按照学号升序排列。
要求把两个链表合并，按照学号升序排列并输出。

输入
第一行有两个用空格隔开的整数n和m，分别表示a和b两个链表中的节点个数。保证n和m均不超过100。
之后的n行每行有两个用空格隔开的整数，分别表示一个学生的学号和成绩。
最后的m行每行有两个用空格隔开的整数，分别表示一个学生的学号和成绩。
输入保证所有的节点中学号各不相同。

输出
合并之后升序排列的链表，每行两个用空格隔开的整数，分别表示一个学生的学号和成绩。
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
    int id;
    int score;
    struct student *next;
} Student;

Student *createNode(int id, int score)
{
    Student *newNode = (struct student *)malloc(sizeof(struct student));
    newNode->id = id;
    newNode->score = score;
    newNode->next = NULL;
    return newNode;
}

Student *insertNode(Student *List, int id, int score)
{
    Student *newNode = createNode(id, score);
    Student *head = List;
    Student *current = List;
    if (id < head->id)
    {
        newNode->next = head;
        return newNode;
    }
    while (current->next != NULL)
    {
        if (current->next->id > id)
        {
            newNode->next = current->next;
            current->next = newNode;
            return head;
        }
        else
        {
            current = current->next;
        }
    }
    current->next = newNode;
    return head;
}

Student *buildList(int n)
{
    int id, score;
    scanf("%d %d", &id, &score);
    Student *head = createNode(id, score);
    for (int i = 1; i < n; i++)
    {
        int newId, newScore;
        scanf("%d %d", &newId, &newScore);
        head = insertNode(head, newId, newScore);
    }
    return head;
}

void printList(Student *List)
{
    Student *head = List;
    Student *current = List;
    while (current != NULL)
    {
        printf("%d %d\n", current->id, current->score);
        current = current->next;
    }
}

int main()
{
    int a, b;
    scanf("%d %d", &a, &b);
    Student *listA = buildList(a);
    Student *listB = buildList(b);
    Student *listC = createNode(listA->id, listA->score);

    Student *current = listA;
    current = current->next;
    for (int i = 1; i < a + b; i++)
    {
        listC = insertNode(listC,current->id,current->score);
        if (current->next == NULL)
        {
            current = listB;
        }else{
            current = current->next;
        }
    }
    printList(listC);
}